package cn.net.asiacom.entity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InvoiceExtractor {
    public static void main(String[] args) {
        String invoiceInfo = "一发票监\n" +
                             "电子发票(统制\n" +
                             "国铁路电子客票)\n" +
                             "国家税务总局\n" +
                             "发票号码:24519146723000010263四\n" +
                             "川开票日期:2024年12月27日\n" +
                             "省税务局\n" +
                             "天府机场站C5563泸州站\n" +
                             "TianfujichangLuzhou\n" +
                             "2024年12月26日09:18开07车14F号二等座\n" +
                             "票价:92.00\n" +
                             "5120812000****219X任鸿杰\n" +
                             "电子客票号:4672363086122690848262024\n" +
                             "购买方名称:北京亚康万玮信息技术股份有限公司统一社会信用代码:91110108663124944W\n" +
                             "买票请到12306发货请到95306\n" +
                             "中国铁路祝您旅途愉快\n" +
                             "全章";

        // 提取始发站、车次、终点站
        Pattern stationPattern = Pattern.compile("(\\p{L}+站)(\\p{L}\\d+)(\\p{L}+站)");
        Matcher stationMatcher = stationPattern.matcher(invoiceInfo);

        // 提取开车日期、时间、座位号、座位等级
        Pattern detailPattern = Pattern.compile("(\\d{4}年\\d{2}月\\d{2}日)\\s*(\\d{2}:\\d{2})开(\\d+车)(\\d+[A-Za-z])号(\\p{L}+座)");
        Matcher detailMatcher = detailPattern.matcher(invoiceInfo);

        // 提取乘客信息
        Pattern passengerPattern = Pattern.compile("\\b(\\d{15}|\\d{18})\\b\\s+([^\\s]+)");
        Matcher passengerMatcher = passengerPattern.matcher(invoiceInfo);

        // 提取电子客票号
        Pattern eTicketPattern = Pattern.compile("电子客票号:(\\d+)");
        Matcher eTicketMatcher = eTicketPattern.matcher(invoiceInfo);

        // 提取购买方名称和统一社会信用代码
        Pattern buyerPattern = Pattern.compile("购买方名称:(.*?)统一社会信用代码:(\\S+)");
        Matcher buyerMatcher = buyerPattern.matcher(invoiceInfo);

        if (stationMatcher.find()) {
            String startStation = stationMatcher.group(1);
            String trainNumber = stationMatcher.group(2);
            String endStation = stationMatcher.group(3);

            System.out.println("始发站: " + startStation);
            System.out.println("车次: " + trainNumber);
            System.out.println("终点站: " + endStation);
        } else {
            System.out.println("未找到匹配的始发站、车次和终点站信息");
        }

        if (detailMatcher.find()) {
            String departureDate = detailMatcher.group(1);
            String departureTime = detailMatcher.group(2);
            String carriageNumber = detailMatcher.group(3);
            String seatNumber = detailMatcher.group(4);
            String seatClass = detailMatcher.group(5);

            System.out.println("开车日期: " + departureDate);
            System.out.println("开车时间: " + departureTime);
            System.out.println("车厢号: " + carriageNumber);
            System.out.println("座位号: " + seatNumber);
            System.out.println("座位等级: " + seatClass);
        } else {
            System.out.println("未找到匹配的开车日期、时间、座位号和座位等级信息");
        }

        if (passengerMatcher.find()) {
            String passengerID = passengerMatcher.group(1);
            String passengerName = passengerMatcher.group(2);

            System.out.println("乘客身份证号: " + passengerID);
            System.out.println("乘客姓名: " + passengerName);
        } else {
            System.out.println("未找到匹配的乘客信息");
        }

        if (eTicketMatcher.find()) {
            String eTicketNumber = eTicketMatcher.group(1);

            System.out.println("电子客票号: " + eTicketNumber);
        } else {
            System.out.println("未找到匹配的电子客票号");
        }

        if (buyerMatcher.find()) {
            String buyerName = buyerMatcher.group(1);
            String socialCreditCode = buyerMatcher.group(2);

            System.out.println("购买方名称: " + buyerName);
            System.out.println("统一社会信用代码: " + socialCreditCode);
        } else {
            System.out.println("未找到匹配的购买方名称和统一社会信用代码");
        }
    }
}